# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： alpha_num_in_tow_rows_template
Description :
Author : 'li'
date： 2020/11/5
-------------------------------------------------
Change Activity:
2020/11/5:
-------------------------------------------------
"""
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.contaner_type_set import \
    CONTAINER_TYPE_SET
from ml.modules.container_num_recognition.single_container_recognition.module.gen_res.container_num_check import verification_container_num


class CommonTemplate:
    def __init__(self, format_result_list):
        self.format_result_list = format_result_list
        self.company_number, self.serial_number, self.check_number = '', '', ''
        self.is_valid_container_number, self.container_type_number = False, ''

    @staticmethod
    def is_this_template():
        """
        is this template
        :return:
        """
        return True  # by default

    def get_result(self):
        """
        get result inference
        :return:
        """
        for index, row in enumerate(self.format_result_list):
            row_content = ''.join(row)
            if len(row_content) > 5 and row_content[:4].isalpha() and row_content[4:].isdigit():
                self.company_number = row_content[:4]
                self.serial_number = row_content[4:]
            if len(row_content) == 7 and row_content[4:].isdigit():
                self.check_number = row_content[6]
                self.serial_number = row_content[:6]
            for item in row:
                if len(item) == 4 and item.isalpha() and item[-1] == 'U':
                    self.company_number = item
                elif len(item) == 6 and item.isdigit():
                    self.serial_number = item
                elif len(item) == 1 and item.isdigit():
                    self.check_number = item
                elif len(item) == 4 and item in CONTAINER_TYPE_SET:
                    self.container_type_number = item
                elif len(item) == 7 and item.isdigit():
                    self.serial_number = item[:6]
                    self.check_number = item[6]
                elif len(item) == 10 and item[:4].isalpha() and item[4:].isdigit():
                    self.company_number = item[:4]
                    self.serial_number = item[4:]
                elif len(item) == 5 and item.isdigit() and self.serial_number == '':
                    self.serial_number = item
                elif len(item) > 4 and item[:4].isalpha() and item[4:].isdigit() \
                        and self.serial_number == '' and self.company_number == '':
                    self.company_number = item[:4]
                    self.serial_number = item[4:]
        if verification_container_num(self.company_number, self.serial_number, self.check_number):
            self.is_valid_container_number = True
        else:
            self.is_valid_container_number = False
